
<template>
  <div class="set">
    <Row type="flex"
         align="middle"
         justify="center"
         class="set_ct">
      <Col>
      <Form ref="formDynamic"
            :model="formDynamic"
            :rules="ruleValidate"
            :label-width="130">
        <FormItem prop='enabledAliPay'
                  label="支付宝显示开关："
                  v-if="loaded">
          <i-switch v-model="formDynamic.enabledAliPay"
                    @on-change="change('1')"
                    style="margin-left:30px">
            <span slot="open">开</span>
            <span slot="close">关</span>
          </i-switch>
        </FormItem>
        <FormItem prop='enabledWechatPay'
                  label="微信显示开关："
                  v-if="loaded">
          <i-switch v-model="formDynamic.enabledWechatPay"
                    @on-change="change('2')"
                    style="margin-left:30px">
            <span slot="open">开</span>
            <span slot="close">关</span>
          </i-switch>
        </FormItem>
        <FormItem prop='enabledUnionPay'
                  label="银联显示开关："
                  v-if="loaded">
          <i-switch v-model="formDynamic.enabledUnionPay"
                    @on-change="change('3')"
                    style="margin-left:30px">
            <span slot="open">开</span>
            <span slot="close">关</span>
          </i-switch>
        </FormItem>
        <FormItem prop='enablePublicTransferPay'
                  label="对公转账显示开关："
                  v-if="loaded">
          <i-switch v-model="formDynamic.enablePublicTransferPay"
                    @on-change="change('4')"
                    style="margin-left:30px">
            <span slot="open">开</span>
            <span slot="close">关</span>
          </i-switch>
        </FormItem>
        <!-- <FormItem>
          <Button type="primary"
                  @click="handleSubmit"
                  class="button">确定</Button>
          <Button @click="cancel"
                  class="button btn1">取消</Button>
        </FormItem> -->
      </Form>
      <div>
        <!-- <FormItem> -->
          <Button type="primary"
                  @click="handleSubmit"
                  class="button">确定</Button>
          <Button @click="cancel"
                  class="button btn1">取消</Button>
        <!-- </FormItem> -->
      </div>
      </Col>
    </Row>
  </div>
</template>

<script>
import './index.less'
// import { routeEqual } from '@/libs/util'
import { getDicQuery, setDicSave } from '@/api/setting'
export default {
  data () {
    return {
      loaded: false,
      btnType: true,
      formDynamic: {
        enabledAliPay: true,
        enabledWechatPay: true,
        enabledUnionPay: true,
        enablePublicTransferPay: true
      },
      ruleValidate: {
        enabledAliPay: [{ required: true, trigger: 'blur' }],
        enabledWechatPay: [{ required: true, trigger: 'blur' }],
        enabledUnionPay: [{ required: true, trigger: 'blur' }],
        enablePublicTransferPay: [{ required: true, trigger: 'blur' }]
      },
      paramter: {
        'shopId': this.$route.query.id,
        'merchantId': this.$store.state.user.merchantId,
        'msType': '1'
      }
    }
  },
  methods: {
    change (idx) {
    },
    handleSubmit () {
      let submitObj = Object.assign({}, this.formDynamic)
      for (let ind in submitObj) {
        if (submitObj[ind]) {
          submitObj[ind] = '1'
        } else {
          submitObj[ind] = '0'
        }
      }
      let data = Object.assign({}, this.paramter, submitObj)
      setDicSave(data).then((res) => {
        if (res.status === 200) {
          this.$Message.success('设置成功!')
          this.closeModal()
        } else {
          this.$Message.error(`${res.data.error}`)
        }
      })
    },
    closeModal () {
      this.$store.commit('closeTag', this.$route)
      // this.$store.commit('setTagNavList', this.$store.state.app.tagNavList.filter(item => !routeEqual(this.$route, item)))
      // this.$nextTick(() => {
      //   this.$router.push({
      //     name: 'store_setting', params: { id: this.$route.query.id }
      //   })
      // })
      if (this.$store.state.app.tagNavList[this.$store.state.app.tagNavList.length - 1].name !== 'store_setting') {
        this.$nextTick(() => {
          this.$router.push({
            name: 'store_setting', params: { id: this.$route.query.id }
          })
        })
      }
    },
    cancel () {
      // this.$refs.formDynamic.resetFields()
      this.closeModal()
    }
  },
  mounted () {
    let obj = Object.assign({}, this.paramter, { 'dictKeys': 'enabledUnionPay,enabledAliPay,enabledWechatPay,enablePublicTransferPay' })
    getDicQuery(obj).then((res) => {
      if (res.data.status === 200) {
        this.loaded = true
        res.data.data.forEach(ele => {
          delete ele.updateTime
          for (let ind in ele) {
            if (ind === 'enabledUnionPay' || ind === 'enabledAliPay' || ind === 'enabledWechatPay' || ind === 'enablePublicTransferPay') {
              if (ele[ind] === '1') {
                ele[ind] = true
              } else {
                ele[ind] = false
              }
            }
          }
        })
        let newObj = Object.assign({}, res.data.data[0], res.data.data[1], res.data.data[2], res.data.data[3])
        this.formDynamic = newObj
      } else {
        this.$Message.error(`${res.data.error}`)
      }
    })
    if (!this.hasPermission('moms:setting:paytype')) {
      this.$router.replace({ name: 'error_401' })
    }
  }
}
</script>
